package com.team.childapp.util;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.provider.MediaStore;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import java.io.OutputStream;

/**
 * Create on: 2024/9/30
 * by Mr.Zhu
 * Description: TODO
 */
public class ImageSaver {
    private static MediaScannerConnection mMediaScannerConnection;
    private static MediaScannerConnection.MediaScannerConnectionClient mClient;

    public static void saveImageToGallery(Context context, String imageUrl, final SaveImageListener saveImageListener) {
        // 创建一个Glide的SimpleTarget
        SimpleTarget<Bitmap> target = new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                saveImageToGallery(context, resource, saveImageListener);
//                saveImageToGalleryQ(context, resource);
            }
        };

        // 使用Glide加载图片
        Glide.with(context)
                .asBitmap()
                .load(imageUrl)
                .into(target);
    }

    private static void saveImageToGallery(Context context, Bitmap bitmap, SaveImageListener saveImageListener) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DISPLAY_NAME, System.currentTimeMillis() + ".jpg");
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.Images.Media.DESCRIPTION, "Image Description");

        // 获取相册相关的Uri
        Uri uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

        LogUtils.showLog("zhu"," uri.getPath(); = "+  uri.getEncodedPath());
        try { if (uri != null) {
            OutputStream outputStream = context.getContentResolver().openOutputStream(uri);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.flush();
            outputStream.close();

            // 通知相册更新
            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            if (saveImageListener != null) {
                saveImageListener.onSuccess();
            }
            scanFile(context,uri.getPath());

        }
        } catch (Exception e) {
            if (saveImageListener != null) {
                saveImageListener.onFail(e.getMessage());
            }
            e.printStackTrace();
        }
    }

    public static void scanFile(Context context,final String filePath) {
        LogUtils.showLog("zhu","filePath = "+ filePath);
        mClient = new MediaScannerConnection.MediaScannerConnectionClient() {
            public void onMediaScannerConnected() {

                mMediaScannerConnection.scanFile(filePath, "image/png");
            }

            public void onScanCompleted(String path, Uri uri) {
                mMediaScannerConnection.disconnect();
            }
        };

        mMediaScannerConnection = new MediaScannerConnection(context, mClient);
        mMediaScannerConnection.connect();
    }


    public interface SaveImageListener {
        void onSuccess();
        void onFail(String error);
    }
}
